package com.platform.auth.security.details.client;

import com.platform.auth.comm.enums.PasswordEncoderTypeEnum;
import com.platform.auth.feign.OAuthClientFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

/**
 * 3、创建客户端信息获取service
 *
 * @author： yuanlai
 */
@Service
@RequiredArgsConstructor
public class ClientDetailsServiceImpl implements ClientDetailsService {
    private final OAuthClientFeignClient oAuthClientFeignClient;

    @Override
    public ClientDetails loadClientByClientId(String clientId) {

        return getOAuth2Client();

        // 通过feign 调用admin服务获取client信息
//        R<OAuth2ClientDTO> result = oAuthClientFeignClient.getOAuth2ClientById(clientId);
//        if (R.ok().getCode().equals(result.getCode())) {
//            OAuth2ClientDTO client = result.getData();
//            BaseClientDetails clientDetails = new BaseClientDetails(
//                    client.getClientId(),
//                    client.getResourceIds(),
//                    client.getScope(),
//                    client.getAuthorizedGrantTypes(),
//                    client.getAuthorities(),
//                    client.getWebServerRedirectUri());
//            clientDetails.setClientSecret(PasswordEncoderTypeEnum.NOOP.getPrefix() + client.getClientSecret());
//            clientDetails.setAccessTokenValiditySeconds(client.getAccessTokenValidity());
//            clientDetails.setRefreshTokenValiditySeconds(client.getRefreshTokenValidity());
//            return clientDetails;
//        } else {
//            throw new NoSuchClientException(result.getMsg());
//        }
    }

    public BaseClientDetails getOAuth2Client() {
        BaseClientDetails clientDetails = new BaseClientDetails(
                "platform",
                "",
                "all",
                //4种模式：密码模式、客户端校验模式、刷新token的模式、授权码模式
                "password,client_credentials,refresh_token,authorization_code",
                "",
                "http://www.baidu.com"
        );
        clientDetails.setClientSecret(PasswordEncoderTypeEnum.NOOP.getPrefix() + "platform");
        clientDetails.setAccessTokenValiditySeconds(3600);
        clientDetails.setRefreshTokenValiditySeconds(36000000);
//        clientDetails.setAutoApproveScopes(null);
        return clientDetails;
    }
}
